home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: To malloc (new) or not to malloc? When is the question.
- Date: 09 Jan 1996 16:18:57 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan9091857@qcd.lanl.gov>
- References: <4ctvk3$ort@maverick.tad.eds.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: fignet05.darrins@eds.com's message of 9 Jan 1996 14:54:59 GMT
-
- --text follows this line--
- In article <4ctvk3$ort@maverick.tad.eds.com> fignet05.darrins@eds.com
- (Darrin Smith) writes:
- <snip>
- Why is it that you can do something like the following:
-
- char *x;
-
- x="Some really long string with no particular meaning";
-
- and have no problems, but can't (safely) do something like this:
-
- <snip>
-
- st1 *sptr; //or struct st1 *sptr in C instead of C++
-
- sptr=fread(....);
-
- <snip>
-
- What is going on here? Why isn't memory set aside for st1 *sptr just as
- it is for char *x? Before I did the new (or malloc) it seemed as though
- my program was getting written over!
-
- Neither is setting aside memory (or rather, to point out an erroneous
- wording on your part, both are setting aside memory for the pointer:
- but the pointer is not pointing anywhere when it is declared.
-
- "Some ... meaning" is a string literal: and string literals in almost
- all uses are arrays of characters. `Array' always has memory allocated
- to it: the characters comprising a string literal sit in statically
- allocated unmodifiable memory.
-
- x = "Some ... meaning" sets the pointer x to be the address of the
- first character of this array.
-
- Similarly if `fread(....)' did return a valid pointer,
- sptr=fread(....) would have been valid. Of course the four dots inside
- the parentheses after fread make absolutely no sense, and it does not
- return a pointer either.
-
- (BEFORE asking questions, read the FAQ available at rtfm.mit.edu. If
- you need to as a question, you should provide a small, complete
- compilable program with a description of what you expected and what
- happened. sptr=fread(....) is just not acceptable.)
-
- Why?
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-